<template>
    <div>
        <!-- 用户列表 -->
        <h1>用户列表</h1>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item><a href="#">首页</a></el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card class="box-card">
            <!-- 搜索 -->
            <div style="margin-top: 15px;">
                <el-input class="ipts" placeholder="请输入内容" v-model="info.query">
                    <el-button slot="append" icon="el-icon-search" @click="searchs"></el-button>
                </el-input>
                <el-button type="primary" class="add" @click="adds">添加用户</el-button>
            </div>


            <!-- 添加弹出框 -->
            <el-dialog title="添加用户" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
                <!-- 表单验证 -->
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <!-- 用户名 -->
                    <el-form-item label="用户名" prop="username" width="100%">
                        <el-input v-model="ruleForm.username"></el-input>
                    </el-form-item>
                    <!-- 密码 -->
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="ruleForm.password"></el-input>
                    </el-form-item>
                    <!-- 邮箱 -->
                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="ruleForm.email"></el-input>
                    </el-form-item>
                    <!-- 手机 -->
                    <el-form-item label="手机" prop="mobile">
                        <el-input v-model="ruleForm.mobile"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
                </span>
            </el-dialog>


            <!-- 修改弹出框 -->
            <el-dialog title="修改用户" :visible.sync="dialogVisible1" width="50%" :before-close="handleClose">
                <!-- 表单验证 -->
                <el-form :model="ruleForm1" :rules="rules1" ref="ruleForm1" label-width="100px" class="demo-ruleForm">
                    <!-- 用户名 -->
                    <el-form-item label="用户名" prop="username" width="100%">
                        <el-input :disabled="true" v-model="ruleForm1.username"></el-input>
                    </el-form-item>
                    <!-- 邮箱 -->
                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="ruleForm1.email"></el-input>
                    </el-form-item>
                    <!-- 手机 -->
                    <el-form-item label="手机" prop="mobile">
                        <el-input v-model="ruleForm1.mobile"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible1 = false">取 消</el-button>
                    <el-button type="primary" @click="submitForm1('ruleForm1')">确 定</el-button>
                </span>
            </el-dialog>




            <!-- 表格 -->
            <el-table ref="singleTable" :data="users" style="width: 100%">
                <!-- 序号 -->
                <el-table-column type="index" label="#" width="50">
                </el-table-column>
                <!-- 姓名 -->
                <el-table-column property="username" label="姓名" width="180">
                </el-table-column>
                <!-- 邮箱 -->
                <el-table-column property="email" label="邮箱" width="180">
                </el-table-column>
                <!-- 电话 -->
                <el-table-column property="mobile" label="电话" width="180">
                </el-table-column>
                <!-- 角色 -->
                <el-table-column property="role_name" label="角色" width="180">
                </el-table-column>
                <!-- 状态 -->
                <el-table-column property="mg_state" label="状态" width="180">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.mg_state" @change="state(scope.row)" active-color="#13ce66"
                            inactive-color="#ff4949">
                        </el-switch>
                    </template>
                </el-table-column>
                <!-- 操作 -->
                <el-table-column property="address" label="操作">
                    <template slot-scope="scope">
                        <!-- 修改 -->
                        <el-button type="primary" size="mini" class="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"></el-button>
                        <!-- 删除 -->
                        <el-button type="danger" size="mini" class="el-icon-delete"
                            @click="handleDelete(scope.$index, scope.row)"></el-button>
                        <!-- 设置 -->
                        <el-button type="warning" size="mini" class="el-icon-s-tools"
                            @click="setap(scope.$index, scope.row)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 删除弹框 -->
            <el-dialog title="提示" :visible.sync="dialogVisible2" width="30%" :before-close="handleClose">
                <span>你确定要删除吗？</span>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="no">取 消</el-button>
                    <el-button type="primary" @click="yes">确 定</el-button>
                </span>
            </el-dialog>


            <!-- 分配角色弹框 -->
            <el-dialog title="分配角色" :visible.sync="setapsl" width="60%" :before-close="handleClose">
                <!-- 内容 -->
                <div class="cont">
                    <div>
                        <span>当前的用户:&emsp;{{ role }}</span>
                    </div>
                    <div>
                        <span>
                            当前的角色:&emsp;{{ setrole }}
                        </span>
                    </div>
                    <div style="margin-top: 15px;">
                        <span>分配新角色:&emsp;</span>
                        <el-select v-model="rid" slot="prepend" placeholder="请选择">
                            <el-option v-for="item in rolearry" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
                        </el-select>
                </div>
                </div>
                
                <span slot="footer" class="dialog-footer">
                    <el-button @click="setaps">取 消</el-button>
                    <el-button type="primary" @click="setaps">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 分页 -->


            <div class="block">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="info.pagenum" :page-sizes="[5, 8, 12, 16]" :page-size="info.pagesize"
                    layout="total, sizes, prev, pager, next, jumper" :total="total">
                </el-pagination>
            </div>
        </el-card>
    </div>
</template>
<script>
import { user, addus, revise, edits, del ,role ,rol} from '../request/Api'
export default {

    data() {
        return {
            // 角色分配
            // rolId:'',
            // 分配角色弹框
            setapsl: false,
            // 删除框
            dialogVisible2: false,
            // 添加框
            dialogVisible: false,
            // 修改框
            dialogVisible1: false,
            // 搜索
            search: '',
            //表格
            users: [],
            info: {
                // 页码
                pagenum: 1,
                // 条数
                pagesize: 5,
                query: ""
            },
            // 状态
            mg_state: true,
            list: [],
            // 添加表单验证
            ruleForm: {
                username: '',
                password: '',
                email: '',
                mobile: ''
            },
            rules: {
                // 用户名
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                // 密码
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                //邮箱
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { pattern: /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, message: '请输入正确的邮箱号', trigger: 'blur' }
                ],
                //手机
                mobile: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '请输入号码为11为的中国大陆号码', trigger: 'blur' }
                ],
            },
            // 一共几页
            total: null,
            // 数据回填
            ids: 0,
            // 修改表单验证
            ruleForm1: {
                username: '',
                password: '',
                email: '',
                mobile: ''
            },
            rules1: {
                //邮箱
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { pattern: /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, message: '请输入正确的邮箱号', trigger: 'blur' }
                ],
                //手机
                mobile: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '请输入号码为11为的中国大陆号码', trigger: 'blur' }
                ],
            },
            // 分配角色定义
            role:'',
            // 当前角色
            setrole:'',
            // 角色分配id
            id:0,
            // 角色分配
            rid:null,
            // 角色列表数组
            rolearry:[],
            // 角色列表名称
            roleNa:[],
        }
    },
    created() {
        this.getlist()
    },
    methods: {
        // 确定角色设置
        setaps(){
            // console.log(this.rid,this.id);
            this.setapsl = false
        },
        // 角色设置
        setap(inde,row) {
            // console.log(inde,row);
            // console.log(this.rolearry.id);
            this.role = row.username
            this.setrole = row.role_name
            this.id = row.id
            this.setapsl = true
            role({id:this.id,rid:this.rid}).then(res=>{
            })
            rol().then(res=>{
                // this.rid = this.rolearry
                this.rolearry = res.data.data
                this.roleNa = res.data.data
                // console.log(this.rolearry);
            })
        },
        // 搜索
        searchs() {
            // console.log(1);
            this.getlist()
        },
        // 添加表单判断
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    addus(this.ruleForm).then(res => {
                        // console.log(res);
                        this.getlist()
                    })
                    this.dialogVisible = false
                    this.$message({
                        message: '添加成功！',
                        type: 'success'
                    });
                } else {
                    this.$message.error('添加失败！')
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },

        // 添加框弹出
        adds() {
            this.dialogVisible = true
        },

        // 修改表单判断
        submitForm1(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    addus(this.ruleForm).then(res => {
                        // 修改传参
                        edits({ id: row.id, email: row.email, mobile: row.mobile }).then(res => {
                            // console.log(res);
                            this.getlist()
                        })
                        this.getlist()
                    })
                    this.dialogVisible1 = false
                    this.$message({
                        message: '修改成功！',
                        type: 'success'
                    });
                } else {
                    this.$message.error('修改失败！')
                    return false;
                }
            });
        },
        resetForm1(formName) {
            this.$refs[formName].resetFields();
        },
        // 关闭添加框
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => { });
        },
        //数据封装list
        getlist() {
            user(this.info).then(res => {
                this.users = res.data.data.users
                this.total = res.data.data.total
            })
        },
        // 编辑
        handleEdit(index, row) {
            this.dialogVisible1 = true
            // console.log(index, row);
            // this.ids = row.id
            // 数据回填
            this.ruleForm1 = row
        },
        // 删除
        handleDelete(index, row) {
            console.log(index, row.id);
            del(row).then(res => {
                console.log(res);
                this.getlist()
            })
            this.dialogVisible2 = true
        },
        // 确定删除
        yes() {
            this.dialogVisible2 = false
            this.$message({
                message: '恭喜你删除成功！',
                type: 'success'
            });
        },
        // 取消删除
        no() {
            this.dialogVisible2 = false
            this.$message.error('已取消删除！');
        },
        // 状态
        state(ste) {
            revise({ id: ste.id, type: ste.mg_state }).then(res => {
                // console.log(res);
            })
            // console.log(ste);
        },
        // 分页
        handleSizeChange(val) {
            console.log(val);
            this.info.pagesize = val
            this.getlist()
        },
        handleCurrentChange(val) {
            this.info.pagenum = val
            this.getlist()
        }
    },
}
</script>
<style lang="scss" scoped>
.cont{
    text-align: left;
    span:nth-child(1){
        padding-top: 50px;
    }
    div:nth-child(2){
        margin-top: 20px;
    }
}
.block {
    float: left;
    margin: 20px 0px;
}

.el-input__inner::v-deep {
    width: 100px;
}

.box-card {
    width: 94%;
    margin: 20px auto;
}

.ipts {
    width: 50%;
    float: left;
}

.add {
    float: left;
    margin-left: 50px;
}
</style>